package cn.edu.scau.cmi.chensicheng.controller;

import org.hibernate.exception.ConstraintViolationException;

import cn.edu.scau.cmi.chensicheng.dao.StudentDAO;
import cn.edu.scau.cmi.chensicheng.dao.TeacherDAO;
import cn.edu.scau.cmi.chensicheng.domain.Student;
import cn.edu.scau.cmi.chensicheng.domain.Teacher;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Tab;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class Controller {
	@FXML
	private Tab addTab;
	@FXML
	private Tab modifyTab;
	@FXML
	private Tab delTab;
	@FXML
	private Tab mtTab;
	@FXML
	private Tab msTab;

	@FXML
	private TextArea textArea;

	@FXML
	private TextField addStuField;
	@FXML
	private TextField addTeaField;
	@FXML
	private Button addStuBtn;
	@FXML
	private Button addTeaBtn;

	@FXML
	private ChoiceBox<?> modifyStuBox;
	@FXML
	private ChoiceBox<?> modifyTeaBox;
	@FXML
	private TextField modifyStuField;
	@FXML
	private TextField modifyTeaField;
	@FXML
	private Button modifyStuBtn;
	@FXML
	private Button modifyTeaBtn;

	@FXML
	private ChoiceBox<?> delStuBox;
	@FXML
	private ChoiceBox<?> delTeaBox;
	@FXML
	private Button delStuBtn;
	@FXML
	private Button delTeaBtn;

	@FXML
	private ChoiceBox<?> mtStuBox;
	@FXML
	private ChoiceBox<?> mtTeaBox;
	@FXML
	private Button mtAddTeaBtn;
	@FXML
	private Button mtModifyTeaBtn;

	@FXML
	private ChoiceBox<?> msStuBox;
	@FXML
	private ChoiceBox<?> msTeaBox;
	@FXML
	private Button msAddStuBtn;
	@FXML
	private Button msDelStuBtn;

	private StudentDAO studentDAO = new StudentDAO();
	private TeacherDAO teacherDAO = new TeacherDAO();

	private ObservableList getStudentItems() {
		return FXCollections.observableArrayList(studentDAO.findAll());
	}

	private ObservableList getTeacherItems() {
		return FXCollections.observableArrayList(teacherDAO.findAll());
	}

	public void initAddTab() {
		addStuBtn.setOnAction(e -> {
			String name = addStuField.getText().trim();
			if (!name.isEmpty()) {
				try {
					studentDAO.save(new Student(name));
					textArea.setText("成功添加学生: " + name);
				} catch (ConstraintViolationException cve) {
					textArea.setText("已存在学生: " + name);
				}
			}
		});
		addTeaBtn.setOnAction(e -> {
			String name = addTeaField.getText().trim();
			if (!name.isEmpty()) {
				try {
					teacherDAO.save(new Teacher(name));
					textArea.setText("成功添加教师: " + name);
				} catch (ConstraintViolationException cve) {
					textArea.setText("已存在教师: " + name);
				}
			}
		});
	}

	public void initModifyTab() {
		modifyTab.setOnSelectionChanged(e -> {
			if (modifyTab.isSelected()) {
				modifyStuBox.setItems(getStudentItems());
				modifyTeaBox.setItems(getTeacherItems());
			}
		});
		modifyStuBtn.setOnAction(e -> {
			String name = modifyStuField.getText().trim();
			if (modifyStuBox.getValue() != null && !name.isEmpty()) {
				try {
					Student student = (Student) modifyStuBox.getValue();
					String oldName = student.getName();
					student.setName(name);
					studentDAO.update(student);
					textArea.setText("成功修改学生姓名:\n" + oldName + " -> " + name);
				} catch (Exception e2) {
					textArea.setText("修改学生姓名失败");
				}
			}
		});
		modifyTeaBtn.setOnAction(e -> {
			String name = modifyTeaField.getText().trim();
			if (modifyTeaBox.getValue() != null && !name.isEmpty()) {
				try {
					Teacher teacher = (Teacher) modifyTeaBox.getValue();
					String oldName = teacher.getName();
					teacher.setName(name);
					teacherDAO.update(teacher);
					textArea.setText("成功修改教师姓名:\n" + oldName + " -> " + name);
				} catch (Exception e2) {
					textArea.setText("修改教师姓名失败");
				}
			}
		});
	}

	public void initDelTab() {
		delTab.setOnSelectionChanged(e -> {
			if (delTab.isSelected()) {
				delStuBox.setItems(getStudentItems());
				delTeaBox.setItems(getTeacherItems());
			}
		});
		delStuBtn.setOnAction(e -> {
			if (delStuBox.getValue() != null) {
				try {
					Student student = (Student) delStuBox.getValue();
					String name = student.getName();
					studentDAO.delete(student);
					textArea.setText("成功删除学生: " + name);
				} catch (Exception e2) {
					textArea.setText("删除学生失败");
				}
			}
		});
		delTeaBtn.setOnAction(e -> {
			if (delTeaBox.getValue() != null) {
				try {
					Teacher teacher = (Teacher) delTeaBox.getValue();
					String name = teacher.getName();
					teacherDAO.delete(teacher);
					textArea.setText("成功删除教师: " + name);
				} catch (Exception e2) {
					textArea.setText("删除教师失败");
				}
			}
		});
	}

	public void initManageTeacherTab() {
		mtTab.setOnSelectionChanged(e -> {
			if (mtTab.isSelected()) {
				mtStuBox.setItems(getStudentItems());
				mtTeaBox.setItems(getTeacherItems());
			}
		});
		mtAddTeaBtn.setOnAction(e -> {
			if (mtStuBox.getValue() != null && mtTeaBox.getValue() != null) {
				try {
					Student student = (Student) mtStuBox.getValue();
					if (student.getTeacher() == null) {
						Teacher teacher = (Teacher) mtTeaBox.getValue();
						student.setTeacher(teacher);
						studentDAO.update(student);
						textArea.setText("成功为 " + student.getName() + " 新增指导教师: " + teacher.getName());
					} else {
						textArea.setText(student.getName() + " 已有指导教师: " + student.getTeacher().getName());
					}
				} catch (Exception e2) {
					textArea.setText("新增指导教师失败");
				}
			}
		});
		mtModifyTeaBtn.setOnAction(e -> {
			if (mtStuBox.getValue() != null && mtTeaBox.getValue() != null) {
				try {
					Student student = (Student) mtStuBox.getValue();
					if (student.getTeacher() != null) {
						Teacher teacher = (Teacher) mtTeaBox.getValue();
						if (teacher.equals(student.getTeacher())) {
							textArea.setText(student.getName() + " 的指导教师已是  " + teacher.getName());
						} else {
							String oldTeacherName = student.getTeacher().getName();
							student.setTeacher(teacher);
							studentDAO.update(student);
							textArea.setText("成功为 " + student.getName() + " 修改指导教师:\n" + oldTeacherName + " -> "
									+ teacher.getName());
						}
					} else {
						textArea.setText(student.getName() + " 还未有指导教师");
					}
				} catch (Exception e2) {
					throw e2;
//					textArea.setText("修改指导教师失败");
				}
			}
		});
	}

	public void initManageStudentTab() {
		msTab.setOnSelectionChanged(e -> {
			if (msTab.isSelected()) {
				msStuBox.setItems(getStudentItems());
				msTeaBox.setItems(getTeacherItems());
			}
		});
		msAddStuBtn.setOnAction(e -> {
			if (msStuBox.getValue() != null && msTeaBox.getValue() != null) {
				try {
					Teacher teacher = (Teacher) msTeaBox.getValue();
					Student student = (Student) msStuBox.getValue();
					if (student.getTeacher() != null) {
						textArea.setText(student.getName() + " 已是 " + student.getTeacher().getName() + " 的教授学生");
					} else {
						teacher.getStudents().add(student);
						student.setTeacher(teacher);
						studentDAO.update(student);
						textArea.setText("成功为 " + teacher.getName() + " 添加教授学生 " + student.getName() + "\n"
								+ teacher.getName() + " 的学生列表: " + teacher.getStudents());
					}
				} catch (Exception e2) {
					textArea.setText("新增教授学生失败");
				}
			}
		});
		msDelStuBtn.setOnAction(e -> {
			if (msStuBox.getValue() != null && msTeaBox.getValue() != null) {
				try {
					Teacher teacher = (Teacher) msTeaBox.getValue();
					Student student = (Student) msStuBox.getValue();
					if (student.getTeacher().equals(teacher)) {
						teacher.getStudents().remove(student);
						student.setTeacher(null);
						studentDAO.update(student);
						textArea.setText("成功为 " + teacher.getName() + " 移除教授学生 " + student.getName() + "\n"
								+ teacher.getName() + " 的学生列表: " + teacher.getStudents());
					} else {
						textArea.setText(student.getName() + " 不是 " + teacher.getName() + " 的教授学生");
					}
				} catch (Exception e2) {
					textArea.setText("移除教授学生失败");
				}
			}
		});
	}

	public void initialize() {
		initAddTab();
		initModifyTab();
		initDelTab();
		initManageTeacherTab();
		initManageStudentTab();
	}
}
